package cn.jiyun.guige.product.service.impl;

import cn.jiyun.common.vo.CommonResult;
import cn.jiyun.guige.product.mapper.ProductGuigeMapper;
import cn.jiyun.guige.product.service.ProductGuigeService;
import cn.jiyun.model.ProductGuige;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ProductGuigeServiceImpl extends ServiceImpl<ProductGuigeMapper,ProductGuige > implements ProductGuigeService {
  @Autowired
    private  ProductGuigeMapper productGuigeMapper;

    @Override
    public Page<ProductGuige> findAll(Integer pageNum, Integer pageSize, ProductGuige productGuige) {
        Page<ProductGuige> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ProductGuige> productGuigeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (productGuige.getName()!=null){
            productGuigeLambdaQueryWrapper.like(ProductGuige::getName,productGuige.getName());
        }
        productGuigeLambdaQueryWrapper.groupBy(ProductGuige::getName);
        Page<ProductGuige>  productGuigePage = productGuigeMapper.selectPage(page, productGuigeLambdaQueryWrapper);
            List<ProductGuige> productGuiges = productGuigeMapper.selectList(null);
            List<ProductGuige> records = productGuigePage.getRecords();
            for (ProductGuige guige : records) {
                String uuu="";
                String ppp="";
                for (ProductGuige productGuige1 : productGuiges) {
                    if (guige.getName().equals(productGuige1.getName())){
                        uuu=uuu + "," +productGuige1.getGuige();
                        ppp=ppp + " " +productGuige1.getShuxing();
                    }
                }
                String substring = uuu.substring(1, uuu.length());
                guige.setGuige(substring);
                String substring1 = ppp.substring(1, ppp.length());
                guige.setShuxing(substring1);
            }
        return productGuigePage;
    }

    @Override
    public CommonResult findAdd(ProductGuige productGuige) {
        LambdaQueryWrapper<ProductGuige> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductGuige::getName,productGuige.getName());
        List<ProductGuige> productGuiges = productGuigeMapper.selectList(queryWrapper);
        if (productGuiges==null){
            productGuigeMapper.insert(productGuige);
        }else{
            queryWrapper.eq(ProductGuige::getGuige,productGuige.getGuige());
            ProductGuige productGuige1 = productGuigeMapper.selectOne(queryWrapper);
            if (productGuige1 ==null){
                productGuigeMapper.insert(productGuige);
            }else{
                String[] split = productGuige1.getShuxing().split(",");
                for (String s : split) {
                    if (s.equals(productGuige.getShuxing())){
                        return CommonResult.error("属性值已存在");
                    }
                }
                productGuige1.setShuxing(productGuige.getShuxing()+","+productGuige1.getShuxing());
                productGuigeMapper.updateById(productGuige1);
            }
        }
        return CommonResult.success();

    }

    @Override
    public CommonResult deletebyid(Integer id) {
        ProductGuige productGuige = productGuigeMapper.selectById(id);
        List<ProductGuige> productGuiges = productGuigeMapper.selectList(null);
        for (ProductGuige guige : productGuiges) {
            if (productGuige.getName().equals(guige.getName())){
                productGuigeMapper.deleteById(guige.getId());
            }
        }
        return CommonResult.success();
    }

    @Override
    public CommonResult deleteIds(Integer[] ids) {
        List<ProductGuige> productGuiges = productGuigeMapper.selectBatchIds(Arrays.asList(ids));
        List<ProductGuige> productGuiges1 = productGuigeMapper.selectList(null);
        ArrayList<Integer> integers = new ArrayList<>();
        for (ProductGuige productGuige1 : productGuiges1) {
            for (ProductGuige productGuige : productGuiges) {
                if (productGuige.getName().equals(productGuige1.getName())){
                    integers.add(productGuige1.getId());
                }
            }
        }
        productGuigeMapper.deleteBatchIds(integers);
        return CommonResult.success();
    }

    @Override
    public CommonResult findUpdate(ProductGuige productGuige) {
        LambdaQueryWrapper<ProductGuige> LambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper.eq(ProductGuige::getName, productGuige.getName());
        List<ProductGuige> productGuiges = productGuigeMapper.selectList(LambdaQueryWrapper);
        String[] split = productGuige.getGuige().split(",");
        String[] split1 = productGuige.getShuxing().split(" ");
        for (ProductGuige guige : productGuiges) {
            for (String s : split) {
                int a=0;
                if (guige.getGuige().equals(s)){
                    a=1;
                    for (String string : split1) {
                        if (guige.getShuxing().equals(string)){
                             continue;
                        }else{
                            guige.setShuxing(string);
                        }
                    }
                    break;
                }
                if (a==1){

                }

            }
            productGuigeMapper.updateById(guige);
        }
        return CommonResult.success();
    }

    @Override
    public List<ProductGuige> findtotal() {
        LambdaQueryWrapper<ProductGuige> productGuigeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productGuigeLambdaQueryWrapper.groupBy(ProductGuige::getName);
        List<ProductGuige> productGuiges = productGuigeMapper.selectList(productGuigeLambdaQueryWrapper);
        for (ProductGuige guige : productGuiges) {
            String uuu="";
            String ppp="";
            for (ProductGuige productGuige1 : productGuiges) {
                if (guige.getName().equals(productGuige1.getName())){
                    uuu=uuu + "," +productGuige1.getGuige();
                    ppp=ppp + " " +productGuige1.getShuxing();
                }
            }
            String substring = uuu.substring(1, uuu.length());
            guige.setGuige(substring);
            String substring1 = ppp.substring(1, ppp.length());
            guige.setShuxing(substring1);
        }
        return productGuiges;
    }

    @Override
    public List<ProductGuige> findBeanByid(Integer id) {
        LambdaQueryWrapper<ProductGuige> productGuigeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productGuigeLambdaQueryWrapper.eq(ProductGuige::getBaseId,id);
        return   productGuigeMapper.selectList(productGuigeLambdaQueryWrapper);

    }
}
